package  com.chint.largeData;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.LayoutParams;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import cn.jpush.android.api.JPushInterface;

public class PageView extends Activity implements OnClickListener {
    private ViewPager viewPager;
    @SuppressWarnings("unused")
    private PagerTitleStrip pagerTitleStrip;//
    private LinearLayout circleGroy;
    private ArrayList<ImageView> mViewList = null;
    private Integer[] mImageDrawableArray = {R.drawable.page1, R.drawable.page2, R.drawable.page3};
    private List<ImageView> imageViews = new ArrayList<ImageView>();
    Button btn_go;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.pageview);
        findViews();
    }
    @Override
	protected void onResume() {
		super.onResume();
		JPushInterface.onResume(this);
	}


	@Override
	protected void onPause() {
		super.onPause();
		JPushInterface.onPause(this);
	}
    private void findViews() {
    	btn_go = (Button) findViewById(R.id.btn_go);
    	btn_go.setOnClickListener(this);
        viewPager = (ViewPager) this.findViewById(R.id.viewpager);
        //viewPager.setPageTransformer(true, new RotateDownPageTransformer());
        circleGroy = (LinearLayout) findViewById(R.id.circleGroy);
        mViewList = new ArrayList<ImageView>();
        LayoutParams params = new LayoutParams();
        params.width = LayoutParams.MATCH_PARENT;
        params.height = LayoutParams.MATCH_PARENT;
        for (int i = 0; i < mImageDrawableArray.length; i++) {
            ImageView imageView = new ImageView(PageView.this);
            imageView.setImageResource(mImageDrawableArray[i]);
            imageView.setScaleType(ScaleType.FIT_XY);
            imageView.setLayoutParams(params);
            this.mViewList.add(imageView);
            if (mViewList.size() - 1 == i) {
                imageView.setOnClickListener(this);
            }
        }
        addImageview();
        viewPager.setOnPageChangeListener(changeListener);
        viewPager.setAdapter(new MyAdapter());

    }

    private void addImageview() {
        for (int i = 0; i < mViewList.size(); i++) {
            ImageView circle = new ImageView(this);
            circle.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            if (i == 0) {
                circle.setImageResource(R.drawable.circleback);
            } else {
                circle.setImageResource(R.drawable.bai);
                circle.setPadding(10, 0, 0, 0);

            }

            imageViews.add(circle);
            circleGroy.addView(circle);

        }
    }

    private OnPageChangeListener changeListener = new OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) {
            for (int i = 0; i < mViewList.size(); i++) {
                if (i == arg0) {
                    imageViews.get(i).setImageResource(R.drawable.circleback);
                } else {
                    imageViews.get(i).setImageResource(R.drawable.bai);
                }
            }
            if(arg0==mViewList.size()-1){
            	btn_go.setVisibility(View.VISIBLE);
            }else{
            	btn_go.setVisibility(View.GONE);
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {


        }
    };

   
    class MyAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mViewList.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // TODO Auto-generated method stub
            ((ViewPager) container).removeView(mViewList.get(position));
        }

        @Override
        public CharSequence getPageTitle(int position) {
            // TODO Auto-generated method stub
            return "";
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // TODO Auto-generated method stub
            ((ViewPager) container).addView(mViewList.get(position));
            return mViewList.get(position);
        }


        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == arg1;
        }

    }
    @Override
    public void onClick(View arg0) {
    	Intent intent = new Intent(PageView.this, HomePage.class);
        startActivity(intent);
        finish();
    }
}
